package br.com.lopes.gci.manager;

import static br.com.lopes.gci.util.Constants.FINALIZANDO_METODO;
import static br.com.lopes.gci.util.Constants.INICIANDO_METODO;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import br.com.lopes.gci.dao.IDominioHibernateDAO;
import br.com.lopes.gci.exception.DAOException;
import br.com.lopes.gci.exception.ManagerException;
import br.com.lopes.gci.model.Dominio;

@Service
public class DominioManager {
	
	private static final Logger LOGGER = Logger.getLogger(DominioManager.class);	
	
	@Autowired
	private IDominioHibernateDAO dominioHibernateDAO ;
		
	public List<Dominio> getDominiosByConstraint(Integer constraint) throws ManagerException{
		LOGGER.debug(INICIANDO_METODO + "getDominiosByConstraint(Integer constraint)");
		try{
			try{
				DetachedCriteria criteria = DetachedCriteria.forClass(Dominio.class);
				criteria.add(Restrictions.eq("constraint.codigo", constraint));
				return dominioHibernateDAO.findByCriteria(criteria);
				
			} catch (DAOException e) {
				throw new ManagerException(e);
			}
		}finally{
			LOGGER.debug(FINALIZANDO_METODO + "getDominiosByConstraint(Integer constraint)");
		}
	}
	
	
	public Dominio findDominioByCodigo(Integer codigo) throws ManagerException{
		LOGGER.debug(INICIANDO_METODO + "findDominioByCodigo(Integer)");
		try{
			try{
				return dominioHibernateDAO.findByPK(codigo);
				
			} catch (DAOException e) {
				throw new ManagerException(e);
			}
		}finally{
			LOGGER.debug(FINALIZANDO_METODO + "findDominioByCodigo(Integer)");
		}
	}
	
}
